package com.stedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.Characters;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.group.UpdateGroup;
import com.stedu.service.CharactersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.validation.constraints.NotBlank;
import javax.validation.groups.Default;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping( "/admin/CharactersServlet")
public class CharactersController  {
    @Autowired
    private CharactersService charactersService;


@PostMapping
    public RespBean addCharacters (@RequestBody @Validated  Characters characters)  {


            charactersService.addcharacters(characters);
            return RespBean.ok("添加成功");

    }
    @PutMapping
    public RespBean upCharacters (@RequestBody @Validated({UpdateGroup.class, Default.class}) Characters characters) throws SteduException {


            charactersService.updatecharacters(characters);
            return RespBean.ok("修改成功");



    }
    @RequestMapping("/{cid}")
    public RespBean delCharacters (@PathVariable("cid") @NotBlank(message = "id不为空") Long cid)  {


            charactersService.delete(cid);
            return RespBean.ok("删除成功");

    }
    @GetMapping("/findAll")
    public RespBean findAll ()  {

            List<Characters> selectall = charactersService.selectall(null,null);
            return RespBean.ok("", selectall);

    }
    @GetMapping("/{cid}")
    public RespBean findById (@PathVariable("cid") @NotBlank(message = "id不为空") Long cid)  {

            Characters characters = charactersService.selectById(cid);


            return RespBean.ok("", characters);


    }
    @GetMapping("/page")
    public RespBean findbypage(@NotBlank Integer pagecou, String cname, String gname) throws ServletException, IOException {
        PageHelper.startPage(pagecou, 5);
        List<Characters> selectall = charactersService.selectall(cname, gname);
        PageInfo<Characters> PageInfo = new PageInfo<>(selectall);
        return RespBean.ok("", PageInfo);

    }
}

